<?php 
require("errorhandle.php");

require("phpconnect.php");

$result = mysql_query("SELECT id,channelname,corder ,pid FROM channel where shelves=0 and pid=-1 order by corder asc");
if(!$result)
{
    mysql_close($con);
    errorHandle($querychannelerror);
	return;
}
$healthnews=new healthnews;

$pindex=0;
while($row = mysql_fetch_array($result))
{
    //echo $row['id'] . " " . $row['channelname'] . " " . $row['pid'];
    $pchnl = new  pchannel;
    //$pchnl->id=$row['id'] ;
    $pchnl->channelname=$row['channelname'] ;
    $healthnews->channels[$pindex]=$pchnl;

    $subresult = mysql_query("SELECT id,channelname,corder ,pid FROM channel where shelves=0 and pid=" . $row['id'] . " order by corder asc");
    if(!$subresult)
    {
	    mysql_close($con);
        errorHandle($querycchannelerror);
	    return;
    }
    $cindex=0;
    while($subrow = mysql_fetch_array($subresult))
    {
	    $childrenchann = new  cchannel;
	    $childrenchann->id=$subrow['id'];
	    $childrenchann->channelname=$subrow['channelname'];
	  
	    $healthnews->channels[$pindex]->childrens[$cindex]=$childrenchann;
	    $cindex++;
    }
    $pindex++;
}
$channle_json = json_encode($healthnews);
if(!$channle_json)
{
    mysql_close($con);
    errorHandle($newsTJsonerror);
	return;
}


mysql_close($con);
echo $channle_json;



class healthnews 
{
    public $channels=array();//频道数组
}
//父频道
class pchannel 
{
    //public $id=0;
	public $channelname ;
	public $childrens=array();//子频道数组
}
//子频道
class cchannel 
{
    public $id=0;
	public $channelname;
}
?>